package com.tywj.panda.main

import android.os.Bundle
import androidx.fragment.app.Fragment
import com.tywj.lib.core.common.util.BarUtil
import com.tywj.lib.core.common.util.ResourcesUtil
import com.tywj.lib.core.common.view.BaseFragment
import com.tywj.lib.route.Route
import com.tywj.lib.route.RouteUtil
import com.tywj.panda.customer.R
import com.tywj.panda.customer.common.provider.IHomeService
import com.tywj.panda.customer.common.provider.ILaunchService
import com.tywj.panda.customer.common.provider.ITravelService
import com.tywj.panda.customer.common.provider.IUserService
import com.tywj.panda.customer.common.route.CommonRoute
import com.tywj.panda.customer.common.tool.LogoutTool
import com.tywj.panda.customer.common.view.base.FragmentChangeManager
import com.tywj.panda.customer.common.view.base.MyBaseActivity
import kotlinx.android.synthetic.main.activity_main_customer.*


/**
 * @desc 用户端主界面
 * @author June Yang
 * @date 2020/5/8
 */
@Route(path = CommonRoute.MAIN_CUSTOMER)
class MainCustomerActivity : MyBaseActivity() {
    companion object {
        //首页
        private const val TAG_HOME = "home"

        //行程
        private const val TAG_TRAVEL = "travel"

        //我的
        private const val TAG_USER = "user"
    }

    private lateinit var homeFragment: BaseFragment
    private lateinit var travelFragment: BaseFragment
    private lateinit var userFragment: BaseFragment

    //fragment集合
    private val fragments = ArrayList<BaseFragment>(3)

    //fragment tag集合
    private val tags = ArrayList<String>(3)

    private lateinit var fragmentChangeManager: FragmentChangeManager

    // 当前选中的RadioButton的Id
    private var curRbId = 0

    override fun getLayoutID(): Int = R.layout.activity_main_customer

    //设置为主界面 logout时判断
    override fun isMainActivity(): Boolean = true

    @Suppress("UNCHECKED_CAST")
    override fun initVariable(savedInstanceState: Bundle?) {
        super.initVariable(savedInstanceState)

        initFragment(savedInstanceState)

        fragmentChangeManager = FragmentChangeManager(
            supportFragmentManager,
            R.id.flMain,
            fragments as ArrayList<Fragment>,
            tags
        )
    }

    override fun initView(savedInstanceState: Bundle?) {
        super.initView(savedInstanceState)
        BarUtil.setStatusBarColor(this, ResourcesUtil.getColor(R.color.common_white))

        //默认显示首页
        rbHome.isChecked = true
        fragmentChangeManager.currentFragmentIndex = 0

        curRbId = R.id.rbHome
    }

    override fun initListener() {
        super.initListener()

        // 切换tab点击监听
        rgNav.setOnCheckedChangeListener { _, checkedId ->
            val preFragment = fragmentChangeManager.currentFragment

            when (checkedId) {
                R.id.rbHome -> {
                    fragmentChangeManager.currentFragmentIndex = 0
                    curRbId = checkedId
                }

                R.id.rbTravel -> {
                    if (LogoutTool.isLogin) {
                        fragmentChangeManager.currentFragmentIndex = 1
                        curRbId = checkedId
                    } else {
                        RouteUtil.getServiceProvider(ILaunchService::class.java)
                            ?.toLoginByPhone()
                            ?.navigation(this)

                        rgNav.check(curRbId)
                    }

                }

                R.id.rbMine -> {
                    fragmentChangeManager.currentFragmentIndex = 2
                    curRbId = checkedId
                }
            }
            if (preFragment != fragmentChangeManager.currentFragment) {
                (preFragment as BaseFragment).onInvisible()
            }
        }
    }

    override fun onResumeExceptFirst() {
        super.onResumeExceptFirst()

        (fragmentChangeManager.currentFragment as BaseFragment).let {
            it.onVisibleEveryTime()
            it.onVisibleExceptFirst()
        }
    }


    private fun initFragment(savedInstanceState: Bundle?) {
        if (savedInstanceState == null) {

            homeFragment = RouteUtil.getServiceProvider(IHomeService::class.java)!!.getHomeFragment(this)
            travelFragment = RouteUtil.getServiceProvider(ITravelService::class.java)!!.getTravelFragment(this)
            userFragment = RouteUtil.getServiceProvider(IUserService::class.java)!!.getUserFragment(this)
        } else {
            homeFragment = supportFragmentManager.findFragmentByTag(TAG_HOME) as BaseFragment
            travelFragment = supportFragmentManager.findFragmentByTag(TAG_TRAVEL) as BaseFragment
            userFragment = supportFragmentManager.findFragmentByTag(TAG_USER) as BaseFragment
        }
        fragments.clear()
        fragments.add(homeFragment)
        fragments.add(travelFragment)
        fragments.add(userFragment)

        tags.clear()
        tags.add(TAG_HOME)
        tags.add(TAG_TRAVEL)
        tags.add(TAG_USER)
    }
}
